**************************************************** 
** Second file to prepare data: Run after prep.do **
**************************************************** 

clear all
set more off


use "pat_gvkey_ct.dta" , clear

gen npat = 1 
gen mean_cites = allcites
gen mean_claims = nclaims

** Identify independent patents (not division or continuation) **
merge m:1 patent using "\USPTO\pat_patapp.dta"
drop if _merge == 2
foreach i of varlist DIV CON CIP{
 replace `i' = 0 if mi(`i')
}
egen strt_yes = rowmax(DIV CON CIP)

drop _merge application_number patent_number 

merge m:1 patent using "\USPTO\parentpat_patapp.dta"
drop if _merge == 2
foreach i  of varlist DIV_parent-CIP_parent{
 replace `i' = 0 if mi(`i')
}
foreach i in DIV CON CIP{
 gen `i'parent_yes  = cond(`i' == 0 , 0 , 1)
}
drop _merge parent_application_number patent_number

gen parent_yes = 1 if CONparent_yes == 1 | CIPparent_yes == 1 | DIVparent_yes == 1
replace parent_yes = 0 if CONparent_yes == 0 & CIPparent_yes == 0 & ///
  DIVparent_yes == 0
  
gen inde_pat = 1 if DIV == 0 & CON == 0 & CIP == 0 & DIV_parent == 0 & ///
 CIP_parent == 0 & DIV_parent == 0
replace inde_pat = 0 if mi(inde_pat) 

** Match patents with claims **
tostring patent, replace 
merge m:1 patent using "\USPTO\patent_document_stats.dta"
drop if _merge == 2
drop _merge

destring patent, replace 
merge m:1 patent using "\NBER\org_gen_76_06.dta"
drop if _merge == 2 
drop _merge 

**Identify triadic patents 
merge m:1 patent using "\other\triadic_pat.dta"
drop if _merge == 2
replace triadic = 0 if _merge == 1
drop _merge 

merge m:1 patent using "ncites_3yr.dta" 
drop if _merge == 2
replace cites_others_3yr = 0 if _merge == 1
drop _merge 

merge m:1 patent using "selfncites_3yr.dta"
drop if _merge == 2
replace cites_self_3yr = 0 if _merge == 1
drop _merge  

bysort pdpass circuit year: egen nnclass = nvals(nclass)

* Calculate quality of independent and strategic patents *
preserve 
keep if inde_pat == 1
gcollapse (sum) triadic npat allcites nclaims /// 
 (mean) nnclass mean_cites mean_claims  ///
 (mean) ct_clm_mean = pat_clm_ct (mean) wrd_min_mean = pat_wrd_min ///
 (mean) orig general (mean) cites_others_3yr_mean =  cites_others_3yr ///
 (sum) cites_others_3yr_sum =  cites_others_3yr ///
 (mean) cites_self_3yr_mean = cites_self_3yr  ///
 (sum)  cites_self_3yr_sum = cites_self_3yr,  by(pdpass circuit year)
foreach i of varlist triadic-cites_self_3yr_sum{
 rename `i' `i'_ind 
 label var `i' "`i' independent patents"
}
tempfile indepat 
save `indepat', replace 
restore 

preserve 
keep if strt_yes == 1
gcollapse (sum) triadic npat allcites nclaims /// 
 (mean) nnclass mean_cites mean_claims  ///
 (mean) ct_clm_mean = pat_clm_ct (mean) wrd_min_mean = pat_wrd_min ///
 (mean) orig general (mean) cites_others_3yr_mean =  cites_others_3yr ///
 (sum) cites_others_3yr_sum =  cites_others_3yr ///
 (mean) cites_self_3yr_mean = cites_self_3yr  ///
 (sum)  cites_self_3yr_sum = cites_self_3yr,  by(pdpass circuit year)
foreach j of varlist triadic-cites_self_3yr_sum{
 rename `j' `j'_strt_yes
 label var `j' "`j' for strategic patents"
}
tempfile  strt_yes
save `strt_yes', replace 
restore 

preserve 
keep if parent_yes == 1
gcollapse (sum) triadic npat allcites nclaims /// 
 (mean) nnclass mean_cites mean_claims  ///
 (mean) ct_clm_mean = pat_clm_ct (mean) wrd_min_mean = pat_wrd_min ///
 (mean) orig general (mean) cites_others_3yr_mean =  cites_others_3yr ///
 (sum) cites_others_3yr_sum =  cites_others_3yr ///
 (mean) cites_self_3yr_mean = cites_self_3yr  ///
 (sum)  cites_self_3yr_sum = cites_self_3yr,  by(pdpass circuit year)
foreach j of varlist triadic-cites_self_3yr_sum{
 rename `j' `j'_parent
 label var `j' "patents with continuations"
}
tempfile parent 
save `parent', replace 
restore 

* Calculating patent quality by business-circuit-year *
gcollapse (sum) triadic npat allcites nclaims ///
 (mean) nnclass mean_cites mean_claims  ///
 (mean) ct_clm_mean = pat_clm_ct (mean) wrd_min_mean = pat_wrd_min ///
 (mean) orig general (mean) cites_others_3yr_mean =  cites_others_3yr ///
 (sum) cites_others_3yr_sum =  cites_others_3yr ///
 (mean) cites_self_3yr_mean = cites_self_3yr  ///
 (sum)  cites_self_3yr_sum = cites_self_3yr,  by(pdpass circuit year)

merge 1:1 pdpass circuit year using `parent'
drop if _merge == 2
foreach j of varlist triadic_parent-cites_self_3yr_sum_parent{
 replace `j' = 0 if mi(`j')
}
drop _merge 

merge 1:1 pdpass circuit year using `strt_yes'
drop if _merge == 2
foreach j of varlist triadic_strt_yes-cites_self_3yr_sum_strt_yes{
 replace `j' = 0 if mi(`j')
}
drop _merge
 
merge 1:1 pdpass circuit year using `indepat'
drop if _merge == 2
foreach j of varlist triadic_ind-cites_self_3yr_sum_ind{
 replace `j' = 0 if mi(`j')
}
drop _merge

* Aggregate to panel by business-circuit-year *
* Fill in variables if business did not patent in circuit-year *

bysort pdpass: egen first_yr = min(year)
bysort pdpass: egen last_yr = max(year)
drop if circuit == 12 

fillin pdpass circuit year
drop if circuit == 11 & year < 1982

gsort pdpass -first_yr
by pdpass: replace first_yr = first_yr[_n-1] if mi(first_yr)
gsort pdpass -last_yr
by pdpass: replace last_yr = last_yr[_n-1] if mi(last_yr)
keep if year >= first_yr & year <= last_yr

order pdpass circuit year 

foreach i of varlist triadic-cites_self_3yr_sum_ind{
 replace `i' = 0 if _fillin == 1
}
drop _fillin

*1. Number of patents in the dominant technology class
merge 1:1 pdpass circuit year using "pd_npatdn.dta" 
drop if _merge == 2
drop _merge 
**Notes: For dtn_3 dtn_5 pd_dtn_3 pd_dtn_5, dominant tech class would be missing
**if the business did not have any 

merge 1:1 pdpass circuit year using "pd_npatds.dta"
drop if _merge == 2
drop _merge 

*2. Match the distribution of companies in same dominant technology class
foreach i in dtn_3 dtn_5 pd_dtn_3 pd_dtn_5{
 rename `i' nclass
 merge m:1 nclass circuit year using "pd_dtn_ctyr.dta", ///
  keepusing(`i'_pd `i'_ct_pd `i'_ct_rate)  
 drop if _merge == 2
 drop _merge 
 rename nclass `i'
}

foreach i in dts_3 dts_5 pd_dts_3 pd_dts_5{ 
 rename `i' subcat
 merge m:1 subcat circuit year using "pd_dts_ctyr.dta", ///
  keepusing(`i'_pd `i'_ct_pd `i'_ct_rate)  
 drop if _merge == 2
 drop _merge 
 rename subcat `i'
}

*3. Match the number of new entrants 
merge 1:1 pdpass circuit year using "newentry_pdct.dta" 
drop if _merge == 2
foreach i in newentry_mean newentry_mean_f3 new_sic_entry_mean new_sic_entry_mean_f3 newentry_sum newentry_sum_f3 newentry_sic_sum newentry_sic_sum_f3{
 replace `i' = 0 if _merge == 1
}
drop _merge 

merge 1:1 pdpass circuit year using "newentry_subcat_pdct.dta" 
drop if _merge == 2
foreach i of varlist newentry_mean_subcat-newentry_sic_sum_f3_subcat {
 replace `i' = 0 if _merge == 1
}
drop _merge 

*4. Match the number of tech class every year 
merge 1:1 pdpass circuit year using "nnclass_pdct.dta"
drop if _merge == 2
drop _merge 

*5. Match the number of continuation patent applications 
merge 1:1 pdpass circuit year using "\USPTO\condivpat_pdct.dta"
foreach i of varlist inde_pat-CIPparent_yes{
 replace `i' = 0 if _merge == 1
}
drop _merge 

*6. Match the number of single-assignee patents 
merge 1:1 pdpass circuit year using "singleass_npat_pdct.dta"
drop if _merge == 2
replace npat_single = 0 if _merge  == 1
drop _merge 

drop gvkey 

merge m:1 pdpass using "\NBER\dynass.dta"
tab _m
keep if _m == 3

gen gvkey =.
forvalue i = 1/5 {
replace gvkey = gvkey`i' if gvkey`i'~=. & year>=begyr`i' & year<=endyr`i'
}
drop if mi(gvkey)
drop _merge

merge m:1 circuit using "\other\CAFC_ct_updated.dta"
keep if _m == 3   
drop _merge

gen CAFC1 = ratepre_all if year < 1983 
replace CAFC1 = ratepost_all if year >= 1986
label var CAFC1 "CAFC index constructed by all cases"

gen CAFC2 = ratepre_home if year < 1983 
replace CAFC2 = ratepost_home if year >= 1986
label var CAFC2 "CAFC index constructed by home circuit cases"

gen CAFC3 = ratepre_pattiff  if year < 1983
replace CAFC3 = ratepost_pattiff if year >= 1986
label var CAFC3 "CAFC index constructed by cases with patentee as the plaintiff"

gen CAFC4 = ratepre_home_pattiff if year < 1983
replace CAFC4 = ratepost_home_pattiff if year >= 1986
label var CAFC4 "CAFC index constructed by cases with patentee as the plaintiff tried at home circuit"

merge m:1 gvkey year using "cmpst_proed.dta"
keep if _merge == 3
drop _merge 

gen hq_circuit = cond(circuit == cmp_circuit, 1, 0)

gen byte manuf = 1 if sic2 >= 20 & sic2 < 40
replace manuf = 0 if mi(manuf)

gen byte defense = 1 if inlist(sic3, 372, 376, 381) 
replace defense = 0 if mi(defense)

merge m:1 gvkey using "\Compustat\gvkey-permno-link.dta"
gen permno = . 
forvalues i = 1/8{
 replace permno = lpermno`i' if !mi(lpermno`i')  & year >= begyr`i' & year <= ///
 endyr`i'    
}
drop if _merge == 2
drop _merge 
drop lpermno1-endd8

merge m:1 permno year using "\Compustat\permno-year-idiovol.dta"
drop if _merge == 2
drop _merge

merge m:1 sic3 year using "\other\HHIfit_HP.dta"
drop if _merge == 2
drop _merge 

egen pd_ct = group(pdpass circuit)
xtset pd_ct year


rename cmp_state cmp_statecode
rename state cmp_state

merge m:1 circuit year using "\other\tsayr_ct.dta", update
drop if _merge == 2
drop _merge 

keep if year >= 1976 & year <= 1992

foreach i of varlist va1_sum-va78_perct{
 gen `i'_ln = ln(`i')
}

xtset pd_ct year 

gen pat_yes = cond(npat == 0, 0 , 1)
replace mean_claims = . if npat == 0

foreach i in mean_claims ct_clm_mean wrd_min_mean {
 gen `i'_inh = asinh(`i')
}

label var tobin "Tobin's Q"
label var emp_ln "Employees"
label var ppe_per_inh  "PPE per employee"
label var revt_per_inh "Revenue per employee"
label var xrd1_inh "R&D expenditure (Missing to 0)"
label var mi_rd "No reported R&D"

gen post = 0 
replace post = 1 if year > =  1983
egen permt_cluster = group(circuit post)
gen CAFC_up = 0 if inlist(circuit, 10, 11)
replace CAFC_up = 1 if mi(CAFC_up)
 
bysort pd_ct post: egen sum_pat_post = sum(npat)

preserve 
keep pd_ct circuit post sum_pat_post
duplicates drop 
gen check = cond(sum_pat_post > 0, 1, 0)
bysort pd_ct: egen everpat_prepost = sum(check)
keep pd_ct everpat_prepost circuit
duplicates drop 
duplicates report 
tempfile temp 
save `temp'
restore 

merge m:1 pd_ct using `temp'
drop _merge 

merge m:1 sic using "\other\concdce_ussic87_isicr3.dta", ///
 keepusing(isicr3)
drop if _m == 2 
drop _m 

gen complex_broad = 0 if isicr3 < 2900  & manuf == 1
replace complex_broad = 1 if isicr3 >= 2900   & manuf == 1 & !mi(isicr3)
replace complex_broad = . if isicr3 >= 3600
replace complex_broad = . if mi(isicr3)
replace complex_broad = . if sic == 3911 
replace complex_broad = 1 if inlist(sic, 3842, 3843, 3861) & !mi(complex_broad)

*complex_broad == 1, 2451 mobile homes
*35 Industrial and Commercial Machinery and Computer Equipment
*36 Electronic and Other Electrical Equipment and Components, Except Computer Equipment
*37 Transportation Equipment
*38 Measuring, Analyzing, and Controlling Instruments; Photographic, Medical and Optical Goods; Watches and Clocks

gen complex_isic = 0 if sic == 2000
 /// food processing industries
replace complex_isic = 0 if sic == 2020
replace complex_isic = 0 if sic == 2030
replace complex_isic = 0 if sic == 2040
replace complex_isic = 0 if sic == 2050
replace complex_isic = 0 if sic == 2060
replace complex_isic = 0 if sic == 2070
replace complex_isic = 0 if sic == 2080
replace complex_isic = 0 if sic == 2090
*2100： tobacco
replace complex_isic = 0  if sic ==  2200 
///Textile Mill Products
replace complex_isic = 0 if sic ==  2250 
///Knitting Mills
replace complex_isic = 0  if sic ==  2390 
///Apparel and Other Finished Products Made From Fabrics and Similar Materials
replace complex_isic =  0 if sic ==  2600
 ///Paper and Allied Products
replace complex_isic =  0 if sic ==  2650
 ///Paperboard Containers and Boxes
replace complex_isic =  0 if sic ==  2670 
///Converted Paper and Paperboard Products, Except Containers and Boxes 
replace complex_isic =  0 if sic ==  2750 
///Commercial Printing
replace complex_isic =  0 if sic ==  2780 
///Blankbooks, Looseleaf Binders, and Bookbinding
replace complex_isic =  0 if sic ==  2790 
///Service Industries For The Printing Trade
replace complex_isic = 0   if sic == 2800 
///Chemicals and Allied Products
replace complex_isic =  0  if sic == 2810 
replace complex_isic =  0  if sic == 2820
replace complex_isic =  0  if sic == 2860
replace complex_isic =  0  if sic == 2870
replace complex_isic =  0  if sic == 2890
replace complex_isic =  0  if sic == 2950
replace complex_isic =  0  if sic == 2990
replace complex_isic =  0  if sic == 3050
replace complex_isic =  0  if sic == 3060
replace complex_isic =  0  if sic == 3080
replace complex_isic =  0  if sic ==  3100
replace complex_isic =  0  if sic == 3140
replace complex_isic =  0   if sic == 3220
replace complex_isic =  0   if sic == 3250
replace complex_isic =  0   if sic == 3260
replace complex_isic =  0   if sic == 3270
 replace complex_isic =  0   if sic ==  3290
replace complex_isic =  0  if sic ==  3310
replace complex_isic =  0  if sic ==  3320
replace complex_isic =  0  if sic ==  3330
replace complex_isic =  0  if sic ==  3350
replace complex_isic =  0  if sic ==  3360
replace complex_isic =  0  if sic ==  3390
replace complex_isic =  0  if sic ==   3420
replace complex_isic =   0 if sic ==   3430
replace complex_isic =  0  if sic ==   3440
replace complex_isic = 0   if sic ==   3460
replace complex_isic =  0  if sic ==   3470
replace complex_isic =  1  if sic ==   3480 
///Ordnance and Accessories, Except Vehicles and Guided Missiles
replace complex_isic =  0 if sic ==   3490
replace complex_isic =  1  if sic >=3510 & sic <= 3590   
 ///Industrial and Commercial Machinery and Computer Equipment
replace complex_isic =  1  if sic >= 3600 &  sic  <  3700  
///Electronic and Other Electrical Equipment and Components, Except Computer Equipment
replace complex_isic =  1   if sic >= 3700 &  sic  <  3800 
/// Transportation Equipment
replace complex_isic =   .  if sic == 3910  
/// Jewelry, Silverware, and Plated Ware
replace complex_isic =  .    if sic  == 3950 
///Pens, Pencils, and Other Artists' Materials
replace complex_isic =   .   if sic == 3960  
///Costume Jewelry, Costume Novelties, Buttons, and Miscellaneous Notions, Ecept Precious Meta
replace complex_isic =   .   if sic == 3990   
///Miscellaneous Manufacturing Industries
replace complex_isic = complex_broad if mi(complex_isic)  & !mi(complex_broad)
replace complex_isic = 1 if sic2 == 38

**Summarzing complex_isic 
*** descrete industries:  
**20 : food 21:tabbaco 22 textile 23 apparel 
**24 wood 
**25. furniture to other manufacturing and excluded 
**26 paper 
*27 Printing, Publishing, and Allied Industries 
**28 Chemicals and Allied Products
**29 Petroleum Refining and Related Industries
**30 Rubber and Miscellaneous Plastics Products
**31 Leather and Leather Products
**32 Stone, Clay, Glass, and Concrete Products
*33 Primary Metal Industriestabk
**34 Fabricated Metal Products, Except Machinery and Transportation Equipment
**38 Measuring, Analyzing, and Controlling Instruments; Photographic, Medical and Optical Goods; Watches and Clocks

*Complx industries
*35 Industrial Machinery & Equipment
*36 Electronic & Other Electric Equipment
*37 Transportation Equipment
*38 Instruments & Related Products

preserve 
use  "\other\ip_cohen.dta" ,clear 
foreach i in pateff_prod pateff_proc secreff_prod secreff_proc{
   egen mean_`i' = mean(`i')
   gen high_`i' = cond(`i' >= mean_`i', 1, 0)
   label var mean_`i' "Average `i' across all isicr3 industries"
   label var high_`i' "Higher `i' (above average) across all isicr3 industries"
}
tempfile cohen 
save `cohen', replace 
restore 

merge m:1 isicr3 using `cohen'
drop if _m == 2
drop _m
replace complex = 1 if inlist(sic, 3842, 3843, 3861) & !mi(complex)

*Discrete: 20 Agricultural Production Livestock and Animal Specialties
*21 Livestock, Except Dairy and Poultry

*br sic isicr4 complex complex_broad complex_isic 
*inconsistency: sic == 3559 Special Industry Machinery, Not Elsewhere Classified complex_IS
replace  complex  = 1 if sic == 3559
replace complex_broad = 1 if sic == 3559 
*inconsistency: sic == 3569 General Industrial Machinery and Equipment, Not Elsewhere Classified Not Elsewhere Classified  
replace  complex  = 1 if sic == 3569
replace complex_broad = 1 if sic == 3569
*sic == 7372 Prepackaged Software, isicr == 2230 Reproduction of recorded media 
*or 7220 Software publishing, consultancy and supply
replace complex  = 1 if sic == 3523
replace complex_broad = 1 if sic == 3523
*sic == 3523 Farm Machinery and Equipment
replace complex  = 1 if sic == 3533
replace complex_broad = 1 if sic == 3533
*sic == 3533 Oil and Gas Field Machinery and Equipment
replace complex = 1 if sic == 3532
replace complex_broad = 1 if sic == 3532
replace complex = 1 if sic ==  3537
replace complex_broad = 1 if sic ==  3537
replace complex = 1 if sic ==  3652
replace complex_broad = 1 if sic ==  3652
replace complex = 1 if sic ==  3695
replace complex_broad = 1 if sic ==  3695
label var complex "complex industries only in cohen et al sample"
label var complex_broad "complex industries defined by isic3 2900 cutoff"
label var complex_isic "complex industries defined by isic3 2900 cutoff and mannually matched for conglomerations"

merge m:1 isic using "\other\cohen_lics.dta"
drop if _m == 2 
drop _m 

merge m:1 isic using "\other\cohen_lics1.dta"
drop if _m == 2 
drop _m 

merge m:1 pdpass using "pd_comcites.dta"
drop if _merge == 2
drop _merge 

merge m:1 sic2 using "sic2_comcites.dta"
drop if _merge == 2
drop _merge

bysort pdpass year : egen nct_npat = nvals(circuit) if npat > 0 
bysort pdpass year: egen temp = mean(nct_npat)
replace nct_npat = temp if mi(nct_npat)
replace nct_npat = 0 if mi(nct_npat)
drop temp

bysort pdpass: egen nct_npat_all = mean(nct_npat)
 
drop ratepre_all_wt  ratepost_all_wt
merge m:1 circuit using "\other\CAFC_ct_updated.dta", update 
replace ratepre_all_wt = 1.958049 if circuit == 11
drop _merge 
compress
save "patent_pdct.dta", replace
 

use "patent_pdct.dta", clear

gen CAFC = gap_all * post  
drop if year >= 1983 & year <= 1985

outreg, clear(table)
global seed 20211013
global reps 1000
global ctrvar emp_ln revt_per_inh ppe_per_inh 
global ctrvar_wrd emp_ln revt_per_inh ppe_per_inh xrd1_inh mi_rd
global sample if manuf == 1

foreach i in mean_claims_strt_yes cites_others_3yr_mean_strt orig_strt_yes ///
  general_strt_yes{
 gen `i'_inh = asinh(`i')  
}

foreach i in cites_others_3yr_mean orig general{
 gen `i'_inh = asinh(`i')
}

gen frag_pdct_wt = frag_pdct * weight_pdct 
replace frag_pdct_wt = 0 if mi(frag_pdct_wt)
bysort sic2 year: egen median_fragpdct = median(frag_pdct_wt)
gen high_fragpdct = cond(frag_pdct_wt > median_fragpdct, 1, 0)
replace high_fragpdct = . if mi(frag_pdct_wt)

gen fragip_pd_wt = frag_pdct_ip * weight_pdct_ip
replace fragip_pd_wt = 0 if mi(fragip_pd_wt)
bysort sic2 year: egen median_fragippd = median(fragip_pd_wt)
gen high_fragippd = cond(fragip_pd_wt > median_fragippd, 1, 0)
replace high_fragippd = . if mi(high_fragippd)

preserve 
keep pdpass year frag_pd weight_pd sic2
duplicates drop 
gen frag_pd_wt = frag_pd * weight_pd
bysort sic2 year: egen median_fragpd = median(frag_pd_wt)
gen high_fragpd = cond(frag_pd_wt > median_fragpd, 1, 0)
replace high_fragpd = . if mi(frag_pd_wt)
tempfile temp
save `temp', replace 
restore 
merge m:1 pdpass year using `temp', nogen

preserve 
global sample if manuf == 1
ppmlhdfe npat CAFC $ctrvar  $sample , ///
    absorb(year pd_ct) cluster(circuit gvkey)  d

duplicates drop gvkey year, force

gen xad_int = xad/revt 
gen ppgt_int = ppegt/revt 

foreach i in xad_int ppgt_int emp_ipolated at_adj ppe_per {
 bysort sic2: egen median_`i' = median(`i')
 gen high_`i' = cond(mi(`i'), ., cond(`i' > median_`i', 1, 0) ) 
}

tempfile contvar
keep gvkey year high_*  
save `contvar', replace
restore
drop _merge 

merge m:1 gvkey year using `contvar'
drop _merge 

tsset, clear 

replace ncites_pd_ct_rate = 0 if ncites_pd_ct == 0  
replace npat_othercmp_ct_rate = 0 if mi(npat_othercmp_ct_rate)
replace othercmp_ct_rate = 0 if mi(othercmp_ct_rate)
replace nciting_pd_ct_rate = 0 if nciting_pd_ct == 0 

 foreach i in ncites_pd_ct_rate nciting_pd_ct_rate npat_othercmp_ct_rate othercmp_ct_rate{
 	sum `i', de 
	gen high_`i' = cond(`i' > `r(p50)', 1, 0)
 }
 
*for firms that do not have forward citations at all 
gen temp = ncites_pd_ct_rate if ncites_pd_ct != 0
replace temp = 1 if ncites_pd_ct == 0 

gen CAFC_home = CAFC * temp
gen CAFC_forshop = CAFC * (1 - temp)

gen CON_DIV =  CON + DIV 

label var inde_pat "Conventional"
label var strt_yes "Strategic"
 
preserve 
use "\other\CAFC_ct_updated.dta", clear
sum ratepre_all
global mean_pre = r(mean)
sum ratepost_all, de
global mean_post = r(mean)
gen high_post = cond(ratepost_all >= `r(p50)', 1, 0)
sum gap_all, de
gen high_increase = cond(gap_all >= `r(p50)', 1, 0)
keep circuit high_increase high_post
tempfile ct 
save `ct', replace 
restore

merge m:1 circuit using `ct', nogen 

gen wrd_min_mean_ln = ln(wrd_min_mean)

preserve 
keep pdpass circuit year npat inde_pat sic2
foreach i in npat inde_pat{ 
bysort pdpass circuit (year): gen sumpre_`i' = sum(`i')
}
keep if year == 1982
drop year 
keep pdpass circuit sumpre_npat sumpre_inde_pat  sic2
duplicates drop 
bysort sic2: egen median_sumpre_inde = median(sumpre_inde_pat)
gen high_sumpre = cond(sumpre_inde_pat > median_sumpre_inde, 1 , 0)
replace high_sumpre = 0 if mi(sumpre_inde_pat)
tempfile temp 
save `temp', replace 
restore 

merge m:1 pdpass circuit using `temp'
drop _merge 
 
compress
save "strt_cont.dta", replace   



